#
# Copyright (C) 2011
#
# Brick Yang <printfxxx@163.com>
#
# This program is free software. You can redistribute it and/or
# modify it as you like.
#

##
# File		kmod.mk
# Brief		Linux kernel module build support
#

ifeq ($(EXTMF_SEG),V)

ifneq ($(origin KDIR),environment)
KDIR = /lib/modules/$(shell uname -r)/build
endif

export KDIR KCFLAGS

endif	# ifeq ($(EXTMF_SEG),V)

ifeq ($(EXTR_SEG),V)

# Copy ko to optdir
define copy_ko_to_opt
$(call copy_to_opt,$$(bdir),$(1),$(1))
endef
# Install ko
define install_ko
install: install-$(1)

install-$(1): $$($(1):%=$$(bdir)%)
	$$(call msg,KINSTALL,$(1))
	unset MAKEFLAGS MAKEFILES CFLAGS ASFLAGS LDFLAGS && \
	$$(MAKE) -C $$(KDIR) M=`readlink -m $$(bdir)` \
	INSTALL_MOD_PATH=$$(DESTDIR) modules_install

.PHONY: install-$(1)
endef

SFX_KO = .ko

KMOD = $(filter %$(SFX_KO),$(MOD))

endif	# ifeq ($(EXTR_SEG),V)

ifeq ($(EXTR_SEG),R)

$(foreach f,$(KMOD),$($(f):%=$(bdir)%)): $(bdir)%: %
	$(call msg,SYMLINK,$*)
	ln -fsnr $< $@

$(foreach f,$(KMOD),$(eval $(bdir)$(f): $($(f):%=$(bdir)%)))

$(KMOD:%=$(bdir)%): $(bdir)%: FORCE
	$(call msg,KBUILD,$*)
	unset MAKEFLAGS MAKEFILES CFLAGS ASFLAGS LDFLAGS && \
	$(MAKE) -C $(KDIR) M=`readlink -m $(bdir)` modules

endif	# ifeq ($(EXTR_SEG),R)
